/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.UserNoticeListDTO;
import org.springblade.modules.admin.entity.UserNotice;
import org.springblade.modules.admin.vo.NoticeTypeVO;
import org.springblade.modules.admin.vo.UserNoticeListTargetVO;
import org.springblade.modules.admin.vo.UserNoticeListVO;
import org.springblade.modules.admin.vo.UserNoticeVO;
import org.springblade.modules.admin.mapper.UserNoticeMapper;
import org.springblade.modules.admin.service.IUserNoticeService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IRoleService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户通知表 服务实现类
 *
 * @author ysq
 * @since 2024-04-01
 */
@Service
public class UserNoticeServiceImpl extends BaseServiceImpl<UserNoticeMapper, UserNotice> implements IUserNoticeService {

	@Autowired
	private IUserService userService;

	@Autowired
	private IRoleService roleService;

	@Override
	public IPage<UserNoticeListVO> pageByCondition(IPage<UserNoticeListVO> page, UserNoticeListDTO userNoticeListDTO) {
		IPage<UserNoticeListVO> pages = baseMapper.pageByCondition(page, userNoticeListDTO);
		// 数据处理，获取当前用户通知列表，统计已读数量
		pages.getRecords().forEach(f -> {
			Long noticeId = f.getNoticeId();
			List<UserNoticeListTargetVO> targets = new ArrayList<>();

			// 获取当前通知的所有用户
			List<UserNotice> list = list(new LambdaQueryWrapper<UserNotice>()
				.eq(UserNotice::getNoticeId, noticeId)
				.eq(BaseEntity::getIsDeleted, 0)
			);
			// 用户封装 VO 所需信息
			list.forEach(t -> {
				UserNoticeListTargetVO target = new UserNoticeListTargetVO();
				target.setUserNoticeId(noticeId);

				User targetUser = userService.getById(t.getUserId());
				if(!Func.isNull(targetUser)){
					target.setUserId(t.getUserId());
					target.setTargetUsername(targetUser.getRealName());
					target.setIsRead(t.getCategory());
					String roleId = targetUser.getRoleId();
					String[] roleIds = roleId.split(",");
					List<String> roleNames = new ArrayList<>();
					for (String roleIdStr : roleIds) {
						if (roleIdStr!= null && !roleIdStr.isEmpty()) {
							if(!Func.isNull(roleService.getById(roleIdStr))){
								String roleName = roleService.getById(roleIdStr).getRoleName();
								roleNames.add(roleName);
							}
						}
					}
					target.setDeptNames(roleNames);
					targets.add(target);
				}
			});
			long count = targets.stream().filter(t -> new Integer(1).equals(t.getIsRead())).count();
			// 一般业务数据不会超出范围，直接类型转换
			f.setReadCount((int)count);
			f.setReadTotalCount(targets.size());
			f.setTargetUsers(targets);
		});
		return pages;
	}

	@Override
	public UserNoticeListVO getDetailById(Long noticeId) {
		UserNoticeListVO userNoticeListVO = baseMapper.getDetailById(noticeId);
		// 数据处理，获取当前用户列表
		List<UserNotice> userNoticeList = this.list(new LambdaQueryWrapper<UserNotice>()
			.eq(UserNotice::getNoticeId, noticeId)
			.eq(BaseEntity::getIsDeleted, 0)
		);
		List<UserNoticeListTargetVO> targets = new ArrayList<>();
		userNoticeList.forEach(t -> {
			UserNoticeListTargetVO target = new UserNoticeListTargetVO();
			target.setUserNoticeId(noticeId);
			User targetUser = userService.getById(t.getUserId());
			target.setUserId(t.getUserId());
			target.setTargetUsername(targetUser.getRealName());
			target.setIsRead(t.getCategory());
			targets.add(target);
		});
		userNoticeListVO.setTargetUsers(targets);
		return userNoticeListVO;
	}

	@Override
	public List<NoticeTypeVO> getNoticeType() {
		return baseMapper.getNoticeType();
	}

}
